package com.atguigu.yjh.cart.controller;

import com.atguigu.yjh.cart.interceptor.CartInterceptor;
import com.atguigu.yjh.cart.service.CartService;
import com.atguigu.yjh.cart.vo.Cart;
import com.atguigu.yjh.cart.vo.CartItem;
import com.atguigu.yjh.cart.vo.UserInfoTo;
import com.atguigu.yjh.common.constant.AuthServerConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutionException;

@Controller
public class CartConller {

    @Autowired
    CartService cartService;

    @GetMapping("/cart.html")
    public String cartListPage(HttpSession session,
                               Model model) throws ExecutionException, InterruptedException {
      Cart cart =  cartService.getCart();
      model.addAttribute("cart",cart);
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(
            @RequestParam("skuId") Long skuId,
            @RequestParam("num") Integer num,
            RedirectAttributes ra) throws ExecutionException, InterruptedException {

       cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);

        return "redirect:http://cart.asxyyjh.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,
                                       Model model){
       CartItem item = cartService.getCartItem(skuId);
       model.addAttribute("item",item);

        return "success";
    }
}
